package com.group.eams.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.eams.mapper.FixedAssetAllMapper;
import com.group.eams.pojo.dto.*;
import com.group.eams.pojo.entity.AssetAllocate;
import com.group.eams.pojo.entity.AssetDispose;
import com.group.eams.pojo.entity.AssetDispose;
import com.group.eams.pojo.vo.*;
import com.group.eams.service.AssetDisposeService;
import com.group.eams.mapper.AssetDisposeMapper;
import com.group.eams.service.FixedAssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 恩哲欣
* @description 针对表【asset_dispose(资产处置表)】的数据库操作Service实现
* @createDate 2024-10-29 16:09:34
*/
@Service
public class AssetDisposeServiceImpl extends ServiceImpl<AssetDisposeMapper, AssetDispose> implements AssetDisposeService{
    
    @Autowired
    private FixedAssetService fixedAssetService;
    @Autowired
    private FixedAssetAllMapper fixedAssetAllMapper;
    @Override
    public Page<AssetDisposeListVo> findAssetDisposeByWhere(AssetDisposeQueryDto assetDisposeQueryDto) {
        Page<AssetDisposeListVo> assetDisposeListVoPage = new Page<>(assetDisposeQueryDto.getPage(),assetDisposeQueryDto.getLimit());
        return getBaseMapper().findAssetDisposeByWhere(assetDisposeListVoPage,assetDisposeQueryDto);
    }

    @Override
    public Page<DisposeFixedListVo> findDisposeFixedByWhere(FixedAssetQueryDto fixedAssetQueryDto) {
        Page<FixedAssetListVo> fixedAssetListVoPage = fixedAssetService.findByQueryDto(fixedAssetQueryDto);
        List<DisposeFixedListVo> disposeFixedListVo = fixedAssetListVoPage.getRecords().stream()
                .map(fixedAssetListVo ->
                        new DisposeFixedListVo(
                                fixedAssetListVo.getId(),
                                fixedAssetListVo.getAssetName(),
                                fixedAssetListVo.getNumber(),
                                fixedAssetListVo.getModel(),
                                fixedAssetListVo.getAssetCategoryName(),
                                fixedAssetListVo.getUnit(),
                                fixedAssetListVo.getAdminName(),
                                fixedAssetListVo.getUserName(),
                                0.00,
                                0.00,
                                0.00,
                                fixedAssetListVo.getNowStorageLocationName(),
                                fixedAssetListVo.getAssetStatus()
                        ))
                .collect(Collectors.toList());
        // 修正这里，添加 records 参数
        Page<DisposeFixedListVo> disposeFixedListVoPage = new Page<>();
        disposeFixedListVoPage.setTotal(disposeFixedListVo.size());
        disposeFixedListVoPage.setRecords(disposeFixedListVo);
        return disposeFixedListVoPage;
    }

    @Override
    public Page<DisposeFixedDetailListVo> addDisposeFixedDetail(AssetPartDto assetPartDto) {
        List<FixedAssetListVo> fixedAssetList = fixedAssetAllMapper.fixedAssetAllListVo();
        List<FixedAssetListVo> fixedAssetListVos = fixedAssetList.stream()
                .filter(fixedAssetListVo -> assetPartDto.getAssetIds().contains(fixedAssetListVo.getId()))
                .collect(Collectors.toList());
        List<DisposeFixedDetailListVo> disposeFixedDetailListVos = fixedAssetListVos.stream().map(fixedAssetListVo ->
                new DisposeFixedDetailListVo(
                        fixedAssetListVo.getId(),
                        fixedAssetListVo.getAssetName(),
                        fixedAssetListVo.getNumber(),
                        fixedAssetListVo.getModel(),
                        fixedAssetListVo.getAssetCategoryName(),
                        fixedAssetListVo.getUnit(),
                        fixedAssetListVo.getAdminName(),
                        fixedAssetListVo.getUserName(),
                        0.00,
                        0.00,
                        0.00,
                        fixedAssetListVo.getAssetStatus(),
                        0.00,
                        fixedAssetListVo.getNowStorageLocationName(),
                        ""
                )).collect(Collectors.toList());
        Page<DisposeFixedDetailListVo> disposeFixedDetailListVoPage = new Page<>();
        disposeFixedDetailListVoPage.setTotal(disposeFixedDetailListVos.size());
        disposeFixedDetailListVoPage.setRecords(disposeFixedDetailListVos);
        return disposeFixedDetailListVoPage;
    }

    @Override
    public Page<DisposeFixedDetailListVo> findDisposeFixedDetail(ApplyNumFixedAssetQueryDto applyNumFixedAssetQueryDto) {
        List<Long> assetIds = getBaseMapper().getAssetIds(applyNumFixedAssetQueryDto.getApplicationNum());
        List<FixedAssetListVo> fixedAssetList = fixedAssetAllMapper.fixedAssetAllListVo();
        List<FixedAssetListVo> fixedAssetListVos = fixedAssetList.stream()
                .filter(fixedAssetListVo -> assetIds.contains(fixedAssetListVo.getId()))
                .collect(Collectors.toList());
        List<DisposeFixedDetailListVo> disposeFixedDetailListVos = fixedAssetListVos.stream().map(fixedAssetListVo ->
                new DisposeFixedDetailListVo(
                        fixedAssetListVo.getId(),
                        fixedAssetListVo.getAssetName(),
                        fixedAssetListVo.getNumber(),
                        fixedAssetListVo.getModel(),
                        fixedAssetListVo.getAssetCategoryName(),
                        fixedAssetListVo.getUnit(),
                        fixedAssetListVo.getAdminName(),
                        fixedAssetListVo.getUserName(),
                        0.00,
                        0.00,
                        0.00,
                        fixedAssetListVo.getAssetStatus(),
                        0.00,
                        fixedAssetListVo.getNowStorageLocationName(),
                        ""
                )).collect(Collectors.toList());
        Page<DisposeFixedDetailListVo> disposeFixedDetailListVoPage = new Page<>();
        disposeFixedDetailListVoPage.setTotal(disposeFixedDetailListVos.size());
        disposeFixedDetailListVoPage.setRecords(disposeFixedDetailListVos);
        return disposeFixedDetailListVoPage;
    }

    @Override
    public int deleteByApplicationNum(BatchDeleteIdsDto batchDeleteIdsDto) {
        // 检查 applicationNum 和 assetIds 是否都不为空，只有都不为空时才进行删除
        if (StrUtil.isEmpty(batchDeleteIdsDto.getApplicationNum()) || CollUtil.isEmpty(batchDeleteIdsDto.getAssetIds())) {
            return 0;  // 如果满足条件之一为空，则不执行删除，返回0
        }

        LambdaQueryWrapper<AssetDispose> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 加入删除条件：applicationNum 非空
        lambdaQueryWrapper.eq(AssetDispose::getApplicationNum, batchDeleteIdsDto.getApplicationNum());

        // 加入删除条件：assetIds 非空
        lambdaQueryWrapper.in(AssetDispose::getAssetId, batchDeleteIdsDto.getAssetIds());

        // 执行删除并返回删除的记录数
        return getBaseMapper().delete(lambdaQueryWrapper);
    }

    @Override
    public int updateByApplicationNum(BatchUpdateIdsDto<AssetDispose> batchUpdateIdsDto) {
        // 检查 applicationNum 和 assetIds 是否都不为空，只有都不为空时才进行修改
        if (StrUtil.isEmpty(batchUpdateIdsDto.getApplicationNum()) ||
                CollUtil.isEmpty(batchUpdateIdsDto.getAssetIds()) ||
                ObjectUtil.isEmpty(batchUpdateIdsDto.getT())) {
            return 0;  // 如果满足条件之一为空，则不执行修改，返回0
        }

        LambdaUpdateWrapper<AssetDispose> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();

        // 设置查询条件
        lambdaUpdateWrapper.eq(AssetDispose::getApplicationNum, batchUpdateIdsDto.getApplicationNum());
        lambdaUpdateWrapper.in(AssetDispose::getAssetId, batchUpdateIdsDto.getAssetIds());

        // 获取要更新的实体对象
        AssetDispose assetDispose = batchUpdateIdsDto.getT();

        // 批量更新字段的方法，避免重复代码
        updateField(lambdaUpdateWrapper, assetDispose.getStatus(), AssetDispose::getStatus);
        updateField(lambdaUpdateWrapper, assetDispose.getTitle(), AssetDispose::getTitle);
        updateField(lambdaUpdateWrapper, batchUpdateIdsDto.getApplicationNum(), AssetDispose::getApplicationNum);
        updateField(lambdaUpdateWrapper, assetDispose.getApplicationDate(), AssetDispose::getApplicationDate);
        updateField(lambdaUpdateWrapper, assetDispose.getApplicantId(), AssetDispose::getApplicantId);
        updateField(lambdaUpdateWrapper, assetDispose.getDisposeDate(), AssetDispose::getDisposeDate);
        updateField(lambdaUpdateWrapper, assetDispose.getDisposeReason(), AssetDispose::getDisposeReason);
        updateField(lambdaUpdateWrapper, assetDispose.getRemark(), AssetDispose::getRemark);
        updateField(lambdaUpdateWrapper, assetDispose.getFlowId(), AssetDispose::getFlowId);
        updateField(lambdaUpdateWrapper, assetDispose.getCurrentNode(), AssetDispose::getCurrentNode);

        // 执行修改并返回修改的记录数
        return getBaseMapper().update(null, lambdaUpdateWrapper);
    }

    @Override
    public AssetDispose findDisposeByNum(String applicationNum) {
        if (StrUtil.isEmpty(applicationNum)) {
            return null;
        }
        return getBaseMapper().findDisposeByNum(applicationNum);
    }

    // 使用 SFunction 进行字段更新
    private <T> void updateField(LambdaUpdateWrapper<AssetDispose> wrapper, T value, SFunction<AssetDispose, T> column) {
        if (value != null) {
            wrapper.set(column, value);
        }
    }

    private <T> void updateField(LambdaUpdateWrapper<AssetDispose> wrapper, T value, T defaultValue, SFunction<AssetDispose, T> column) {
        if (value != null && !value.equals(defaultValue)) {
            wrapper.set(column, value);
        }
    }
}
